package org.googlecode.viewt.plugin.ui.internal.adt.design;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.ui.IEditorPart;
import org.googlecode.viewt.plugin.ui.internal.adt.editor.CommonSelectionManager;
import org.googlecode.viewt.plugin.ui.internal.editor.ConstantMultiPageEditor;

public class DesignViewGraphicalViewer extends ScrollingGraphicalViewer {

	protected InputChangeManager inputChangeManager = new InputChangeManager();

	private IEditorPart editorPart;
	
	public DesignViewGraphicalViewer(
			IEditorPart editor, CommonSelectionManager manager) {
		super();
		this.editorPart = editor;
//		setContextMenu(new DesignViewContextMenuProvider(editor, this, this));
//		editor
//				.getEditorSite()
//				.registerContextMenu(
//						"org.eclipse.wst.xsd.ui.popup.graph", getContextMenu(), internalSelectionProvider, false); //$NON-NLS-1$
// 		make the internalSelectionProvider listen to graph view selection
// 		changes
//		addSelectionChangedListener(internalSelectionProvider);
//		internalSelectionProvider.addSelectionChangedListener(manager);
//		manager.addSelectionChangedListener(this);
//
//		setKeyHandler(new BaseGraphicalViewerKeyHandler(this));
	}

	// @Override
	// public void selectionChanged(SelectionChangedEvent event) {
	// // TODO Auto-generated method stub
	//
	// }

	public void addInputChangdListener(ISelectionChangedListener listener) {
		inputChangeManager.addSelectionChangedListener(listener);
	}

	private class InputChangeManager implements ISelectionProvider {
		List<ISelectionChangedListener> listeners = new ArrayList<ISelectionChangedListener>();

		public void addSelectionChangedListener(
				ISelectionChangedListener listener) {
			if (!listeners.contains(listener)) {
				listeners.add(listener);
			}
		}

		public ISelection getSelection() {
			// no one should be calling this method
			return null;
		}

		public void removeSelectionChangedListener(
				ISelectionChangedListener listener) {
			listeners.remove(listener);
		}

		public void setSelection(ISelection selection) {
			notifyListeners(selection);
		}

		void notifyListeners(ISelection selection) {
			List<ISelectionChangedListener> list = new ArrayList<ISelectionChangedListener>(
					listeners);
			for (Iterator<ISelectionChangedListener> i = list.iterator(); i
					.hasNext();) {
				ISelectionChangedListener listener = i.next();
				listener.selectionChanged(new SelectionChangedEvent(this,
						selection));
			}
		}
	}
}
